//
//  JSWULIUViewCell.swift
//  fho
//
//  Created by 郭颢源 on 2021/5/18.
//

import UIKit
import WebKit

class JSWULIUViewCell: UITableViewCell {
    
    var Telephone :String = ""
    lazy var imgView: UIImageView = {
        var imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        return imgView
    }()
    lazy var title:UILabel = {
        var title = UILabel()
        title.font = UIFont.boldSystemFont(ofSize: 18)
        return title
    }()
    lazy var content:UILabel = {
        var lab = UILabel()
        lab.numberOfLines = 0
        lab.lineBreakMode = .byWordWrapping
        return lab
    }()
    lazy var btn:UIButton = {
        var btn = UIButton()
        btn.setImage(UIImage(named: "tel"), for: .normal)
        return btn
    }()
    let cellMargin = 10
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(imgView)
        contentView.addSubview(title)
        contentView.addSubview(content)
        contentView.addSubview(btn)
        imgView.snp.makeConstraints { (make) in
            make.top.left.equalTo(cellMargin)
            make.bottom.equalTo(-cellMargin)
            make.width.equalTo(imgView.snp.height).multipliedBy(1.2)
        }
        title.snp.makeConstraints { (make) in
            make.top.equalTo(self.cellMargin)
            make.left.equalTo(imgView.snp.right).offset(10)
            make.height.equalToSuperview().multipliedBy(0.3)
            make.right.equalTo(-30)
        }
        content.snp.makeConstraints { (make) in
            make.top.equalTo(title.snp.bottom)
            make.left.right.equalTo(title)
            make.bottom.equalTo(-self.cellMargin)
        }
        btn.snp.makeConstraints { (make) in
            make.right.equalTo(-self.cellMargin)
            make.width.height.equalTo(25)
            make.bottom.equalTo(contentView.snp.centerY)
        }
        btn.addTarget(self, action: #selector(clickTel), for: .touchUpInside)
//        showSubviews()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func clickTel()
    {
        
//        if case 11...20 = Telephone.count {
//            let callWebview = WKWebView()
//            callWebview.load(URLRequest(url: URL(string: "tel:"+Telephone.trim())!))
//            contentView.addSubview(callWebview)
//        }
//        if Telephone.count>11 {
//
//        }
        let tels = Telephone.split(separator: " ")
        
//        print(Telephone.split(separator: " "))
        
        // phoneStr:  电话号码
        //         let phone = "telprompt://" + Telephone
        //         if UIApplication.shared.canOpenURL(URL(string: phone)!) {
        //              UIApplication.shared.openURL(URL(string: phone)!)
        //          }

        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        tels.forEach { (tel) in
            
            alert.addAlertAction(withTitle: String(tel).trim()) { (action) in
                let callWebview = WKWebView()
                callWebview.load(URLRequest(url: URL(string: "tel:"+String(tel).trim())!))
                self.contentView.addSubview(callWebview)
            }
        }
        let action = UIAlertAction(title: "取消", style: .cancel) { (action) in
            alert.dismiss(animated: true, completion: nil)
        }
        alert.addAction(action)
        alert.show()
        
        
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
